package com.onion.utils.utils;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;

@SuppressWarnings("all")
public class DateHelper {

    private static final Logger logger = LoggerFactory.getLogger(DateHelper.class);

    private static final String MON_FMT = "yyyy-MM";
    private static final String DAY_FMT = "yyyy-MM-dd";

    public static final String DAY_STAMP_FMT = "yyyyMMdd";
    public static final String MONTH_STAMP_FMT = "yyyyMM";


    /**
     * 格式化日期
     * @param date
     * @param format
     * @return
     */
    public static String format(Date date, String format){
        if(date == null){
            throw new RuntimeException("日期为空!");
        }
        SimpleDateFormat sdf = new SimpleDateFormat(format);
        return sdf.format(date);
    }

    /**
     * 得到sDate月的day天
     * @param sDate
     * @param day
     * @return
     */
    public static Date getDateOfAccumulate(String sDate, int day) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMM");
        Calendar calendar = Calendar.getInstance();
        try {
            calendar.setTime(sdf.parse(sDate));
            calendar.set(Calendar.DAY_OF_MONTH, day);
            return calendar.getTime();
        } catch (ParseException e) {
            throw new RuntimeException(e);
        }
    }

    /**
     * 得到date前num个月的字符串值
     * @param date
     * @param num
     * @return
     */
    public static String getMonthFromDate(Date date, int num) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.add(Calendar.MONTH, -num);
        SimpleDateFormat dateformat = new SimpleDateFormat("yyyyMM");
        return dateformat.format(calendar.getTime());
    }

    /**
     * 转换日期格式
     * @param date
     * @param format: "yyyy-MM-dd HH:mm:ss.SSS"
     * @return
     */
    public static String getDateFormat(Date date, String format) {
        SimpleDateFormat df = new SimpleDateFormat(format);
        return df.format(date);
    }

    /**
     * 时间秒操作
     * @param date
     * @param addValue
     * @return
     */
    public static Date getDateAddSecond(Date date, int addValue) {
        Calendar calendar = Calendar.getInstance(Locale.CHINA);
        calendar.setTime(date);
        calendar.add(Calendar.SECOND, addValue);
        return calendar.getTime();
    }

    /**
     * 时间分操作
     * @param date
     * @param addValue
     * @return
     */
    public static Date getDateAddMinute(Date date, int addValue) {
        Calendar calendar = Calendar.getInstance(Locale.CHINA);
        calendar.setTime(date);
        calendar.add(Calendar.MINUTE, addValue);
        return calendar.getTime();
    }

    /**
     * 时间分操作
     * @param date
     * @param addValue
     * @return
     */
    public static Date getDateAddHour(Date date, int addValue) {
        Calendar calendar = Calendar.getInstance(Locale.CHINA);
        calendar.setTime(date);
        calendar.add(Calendar.HOUR, addValue);
        return calendar.getTime();
    }

    /**
     * 日期天操作
     *
     * @param date
     * @param addValue
     * @return
     */
    public static Date getDateAddDay(Date date, int addValue) {
        Calendar calendar = Calendar.getInstance(Locale.CHINA);
        calendar.setTime(date);
        calendar.add(Calendar.DATE, addValue);
        return calendar.getTime();
    }

    /**
     * 日期月操作
     *
     * @param date
     * @param addValue
     * @return
     */
    public static Date getDateAddMonth(Date date, int addValue) {
        Calendar calendar = Calendar.getInstance(Locale.CHINA);
        calendar.setTime(date);
        calendar.add(Calendar.MONTH, addValue);
        return calendar.getTime();
    }

    /**
     * 日期年操作
     *
     * @param date
     * @param addValue
     * @return
     */
    public static Date getDateAddYear(Date date, int addValue) {
        Calendar calendar = Calendar.getInstance(Locale.CHINA);
        calendar.setTime(date);
        calendar.add(Calendar.YEAR, addValue);
        return calendar.getTime();
    }

    public static Date getDateMonthFirstDay(Date date, int addValue) {
        Date nextMonth = getDateAddMonth(date, addValue);
        return getDateMonthFirstDay(nextMonth);
    }

    public static Date getDateMonthLastDay(Date date, int addValue) {
        Date nextMonth = getDateAddMonth(date, addValue);
        return getDateMonthLastDay(nextMonth);
    }

    public static Date getDateMonthFirstDay(Date date) {
        Calendar calendar = Calendar.getInstance(Locale.CHINA);
        calendar.setTime(date);
        calendar.set(Calendar.DAY_OF_MONTH, 1);
        calendar.set(Calendar.HOUR_OF_DAY, 0);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);
        return calendar.getTime();
    }

    public static Date getDateMonthLastDay(Date date) {
        Date nextMonth = getDateAddMonth(date, 1);
        Calendar calendar = Calendar.getInstance(Locale.CHINA);
        calendar.setTime(nextMonth);
        calendar.set(Calendar.DAY_OF_MONTH, 1);
        calendar.set(Calendar.HOUR_OF_DAY, 0);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);
        calendar.add(Calendar.SECOND, -1);
        return calendar.getTime();
    }

    /**
     * 日期字符串转日期类型
     * @param date
     * @param format
     * @return
     * @throws ParseException
     */
    public static Date getDate(String date, String format) throws ParseException {
        SimpleDateFormat df = new SimpleDateFormat(format);
        return df.parse(date);
    }

    /**
     * 日期起始时间
     */
    public static Date getDateStart(Date date){
        String start = getDateFormat(date, "yyyyMMdd");
        try{
            return getDate(start, "yyyyMMdd");
        }catch(Exception exp){
            throw new RuntimeException(exp);
        }
    }

    /**
     * 日期截至时间-毫秒
     */
    public static Date getDateEnd(Date date){
        String start = getDateFormat(date, "yyyyMMdd");
        try{
            return getDate(start+"235959999", "yyyyMMddHHmmssSSS");
        }catch(Exception exp){
            throw new RuntimeException(exp);
        }
    }

    /**
     * 获取时间间隔：单位：毫秒
     * @Description:
     * @param date1
     * @param date2
     * @return
     */
    public static long getDateDiff(Date date1, Date date2){
        return date1.getTime()-date2.getTime();
    }

    /**
     * 判断时间是否相同
     * @Description:
     * @param date1
     * @param date2
     * @return
     */
    public static boolean equal(Date date1, Date date2){
        return date1.getTime() == date2.getTime();
    }

    /**
     * 获取重叠交叉的时间段
     * @param begin1
     * @param end1
     * @param begin2
     * @param end2
     * @return
     */
    public static Date[] overlapTime(Date begin1, Date end1, Date begin2, Date end2){
        Date[] section = new Date[2];
        section[0] = begin1.before(begin2)?begin2:begin1;
        section[1] =  end1.before(end2)?end1:end2;
        return section;
    }


}
